import Vue from "vue";
import Vuex from "vuex";
import router from "../router";
import createPersistedState from "vuex-persistedstate";
import { MenuList } from "@/utils/list";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    isMobile: "pc", // 客户端是pc还是wap
    MenuList, // 列表内容
    TopMenuActive: 0, // 一级导航
    SecondaryMenuActive: "/AfterSales/Operate", // 二级导航
    UserId: "", // userid
    CurrentItem: {}, // 点击修改的当前列数据
  },
  mutations: {
    SETISMOBILE(state, Mobile) {
      state.isMobile = Mobile;
    },
    SETTOPMENUACTIVE(state, item) {
      // console.log(item);
      state.TopMenuActive = item.index;
      state.SecondaryMenuActive = item.url;
      router.push(item.url);
      //console.log(state.SecondaryMenuActive);
    },
    SETSECONDARYMENUACTIVE(state, active) {
      state.SecondaryMenuActive = active.url;
    },
    SETUSERID(state, UserId) {
      state.UserId = UserId;
    },
    RESERSTATE(state) {
      state.TopMenuActive = 0;
      state.SecondaryMenuActive = "/AfterSales/Operate";
      state.MenuList = MenuList;
    },
    SETCURRENITEM(state, Item) {
      state.CurrentItem = Item;
    },
  },
  actions: {
    // 初始化
    ResetState(context) {
      context.commit("RESERSTATE");
    },
    // 设置顶级
    SetTopMenuActive(context, item) {
      //console.log(item);
      context.commit("SETTOPMENUACTIVE", item);
    },
    // 设置二级
    SetSecondaryMenuActive(context, active) {
      // console.log("shezhil1");
      context.commit("SETSECONDARYMENUACTIVE", active);
    },
    // 设置是电脑端还是手机端
    SetIsMobile(context, Mobile) {
      context.commit("SETISMOBILE", Mobile);
    },
    // 设置UserId
    SetUserId(context, UserId) {
      context.commit("SETUSERID", UserId);
    },
    // 设置当前修改的列
    SetCurrentItem(context, Item) {
      context.commit("SETCURRENITEM", Item);
    },
  },
  modules: {},
  plugins: [
    createPersistedState({
      key: "myAppStore",
    }),
  ],
});
